package com.eleven.trades.serverimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleven.trades.entity.Account;
import com.eleven.trades.entity.Flow;
import com.eleven.trades.mapper.AccountMapper;
import com.eleven.trades.mapper.FlowMapper;
import com.eleven.trades.result.ResultCode;
import com.eleven.trades.server.AccountServer;
import com.eleven.trades.utils.EncodeByMd5;
import com.eleven.trades.vo.AccountBalance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author linhaizeng
 */
@Service
public class AccountServerImpl implements AccountServer {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private FlowMapper flowMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String transfer(String accountA, String accountB, Double balance) {
        try {
            LocalDateTime start_time = LocalDateTime.now();
            QueryWrapper<Account> qw1 = new QueryWrapper<>();
            QueryWrapper<Account> qw2 = new QueryWrapper<>();

            qw1.lambda().eq(Account::getAccountId,accountA);
            qw2.lambda().eq(Account::getAccountId,accountB);

            Account accountSrc = accountMapper.selectOne(qw1);
            Account accountDest = accountMapper.selectOne(qw2);

//            Account accountSrc = accountMapper.selectById(accountA);
//            Account accountDest = accountMapper.selectById(accountB);

            // 判断余额是否大于转账金额
            if (accountSrc.getAccountBalance()-balance > 0 ) {
                accountMapper.updateBalance(accountA,accountSrc.getAccountBalance()-balance);
                accountMapper.updateBalance(accountB,accountDest.getAccountBalance()+balance);
                // 余额判断
                LocalDateTime end_time = LocalDateTime.now();
                Flow flow = new Flow();
                flow.setInId(accountA);
                flow.setOutId(accountB);
                flow.setFlowStatus(1);
                flow.setNote("交易成功");
                flow.setStartTime(start_time);
                flow.setEndTime(end_time);
                flow.setFlowAmount(balance);
                flowMapper.insert(flow);
                return ResultCode.SUCCESS.getMessage();
            }else {
                Flow flow = new Flow();
                flow.setInId(accountA);
                flow.setOutId(accountB);
                flow.setFlowStatus(0);
                flow.setNote("余额不足 交易失败");
                flow.setStartTime(start_time);
                flow.setEndTime(start_time);
                flow.setFlowAmount(balance);
                flowMapper.insert(flow);
                return ResultCode.COMMON_FAIL.getMessage();
            }
        } catch (Exception e) {
            throw new RuntimeException("出现异常"+e);
        }
    }

    /**
     * 验证账户号与账户名是否匹配
     * @param name
     * @param account
     * @return
     */
    @Override
    public boolean signAccount(String name, String account) {
        String signName = accountMapper.selectName(account);
        if (!signName.equals(name)){
            return false;
        }
        return true;
    }

    /**
     * 查询余额
     * @param accountA 账户A
     * @return
     */
    @Override
    public Double selectBalance(String accountA) {
        return null;
    }

    /**
     * 根据账户查询余额
     * @param user_id
     * @return
     */
    @Override
    public List<AccountBalance> selectAccountBalance(Integer user_id) {
        try {
            List<AccountBalance> accountBalances = accountMapper.selectByAccountId(user_id);
            return accountBalances;
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 验证账户号与密码
     * @param accountId
     * @param passWord
     * @return
     */
    @Override
    public boolean validPassPassword(String accountId, String passWord) {
//        String s = accountMapper.selectPPassword(accountId);
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.lambda().eq(Account::getAccountId,accountId);
        String s = accountMapper.selectOne(qw).getPayPassword();
        System.out.println(s);
        if (EncodeByMd5.encode(passWord).equals(s)) {
            return true;
        } else {
            return false;
        }
    }
}
